package bhbt.controller.admin.member;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Admin;
import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.service.DailiService;
import bhbt.service.DailiShengService;
import bhbt.service.MemberService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;






@Controller
 
public class DailiShengController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private DailiService dailiService;
	@Resource
	private DailiShengService dailiShengService;

	@RequestMapping("admin/member/dailiShengShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//获取list
		Map formMap = getParameterMap(request);
		
		
		List dailiList = dailiService.queryForList("select", formMap);
		
		 
		request.setAttribute("dailiList",dailiList);
		
		return "admin/member/dailiSheng";
	}

	@RequestMapping("admin/member/dailiSheng.shtml")
	public String update(HttpServletRequest request, ModelMap model) throws Exception{
	        
	    	
    	Map formMap = getParameterMap(request);
      
        String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
        
        boolean result =true;  //验证结果
      
    	Admin admin = ContextUtil.getAdmin();
    	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
    	 
    	if(!admin.getTwoPassword().equals(pwd)){
    		setMessage(model,"管理员安全密码不正确。");
    		result=false;
    		 
    		
    	}else{
    		String loginName = MapUtil.getString(formMap, "loginName");
    		Member member = memberService.executMemberByLoginName(loginName);
    		 
    		if(member==null){
    			setMessage(model,"会员账号不存在。");
        		result=false;
    		}else{
    			int dailiId = MapUtil.getIntValue(formMap, "dailiId");
    			Daili daili = this.dailiService.getByKey(dailiId);
    			this.dailiShengService.saveSheng(member, daili);
    		}
    			
    		 
    	} 
    	if(!result){
    		List dailiList = dailiService.queryForList("select", formMap);
    		
   		 
    		request.setAttribute("dailiList",dailiList);
    		
    		return "admin/member/dailiSheng";
		}
    	
    	return "redirect:dailiShengList.shtml";
    }
	 

}
